<?php
/**
 * 主要功能已放到后台 customer权限下了处理
 * @see \App\Http\Controllers\Admin\Customer\PackCategoryController
 *
 * Unity PC 端素材制作软件 整体妆容类别CURD
 *
 * Author: hezhizheng
 * Created by Panxsoft.
 */

namespace App\Http\Controllers\PC\CustomerStore;

use App\Http\Controllers\ApiController;
use App\Http\Requests\PC\CustomerStore\PackCategory\PackCategoryStoreRequest;
use App\Http\Requests\PC\CustomerStore\PackCategory\PackCategoryUpdateRequest;
use App\Models\Store\PackCategory;
use App\Services\Utils\Transaction;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;

class PackCategoryController extends ApiController
{
    /**
     * 显示/查询所有整体妆容类别
     *
     * @param Request $request
     *
     * @param $customer_id
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, int $customer_id)
    {
        /** @var LengthAwarePaginator|PackCategory[] $pack_categories */
        $pack_categories = (new PackCategory)
            ->where(compact('customer_id'))
            ->with('latestPack')
            ->latest()
            ->paginate($request->per_page ?: 15);

        // use first pack thumb as pack category image then hide pack
        $pack_categories->each(function (PackCategory $pack_cat) {
            $pack_cat->makeHidden('latestPack');
        });

        return $this->response(compact('pack_categories'));
    }

    /**
     * @param PackCategoryStoreRequest $request
     * @param int $customer_id
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function store(PackCategoryStoreRequest $request, int $customer_id)
    {
        $pack_data = [
            'name'        => $request->name,
            'customer_id' => $customer_id,
        ];

        $stored = Transaction::handle(function () use ($pack_data) {
            /** @var PackCategory $pack_category */
            $pack_category = (new PackCategory)->create($pack_data);

            return $pack_category;
        });

        return $this->response(compact('stored'));
    }


    /**
     * @param PackCategoryUpdateRequest $request
     * @param int $customer_id
     * @param $pack_category_id
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function update(PackCategoryUpdateRequest $request, int $customer_id, $pack_category_id)
    {
        /** @var PackCategory $pack_category */
        $pack_category = (new PackCategory)->where(compact($customer_id))
            ->find($pack_category_id);
        $this->checkItemExist($pack_category, trans('pc.pack_category'));

        $pack_data = [
            'name' => $request->name,
        ];

        $updated = Transaction::handle(function () use ($pack_category, $pack_data) {

            $pack_category->update($pack_data);

            return $pack_category;
        });

        return $this->response(compact('updated'));
    }


    /**
     * @param int $customer_id
     * @param $pack_category_id
     * @return array|\Illuminate\Http\Response
     * @throws /Exception
     */
    public function destroy(int $customer_id, $pack_category_id)
    {
        /** @var PackCategory $pack_category */
        $pack_category = (new PackCategory)->where(compact($customer_id))
            ->find($pack_category_id);
        $this->checkItemExist($pack_category, trans('pc.pack_category'));

        $pack_category->packs->pluck('pack_category_id');

        $this->checkIsTrue($pack_category->packs()->count()==0 ,trans('pc.pack_category_used'));

        $deleted = $pack_category->delete();

        return $this->response(compact('deleted'));
    }
}
